package com.example.testrxjava.fragment.person;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.testrxjava.IndexActivity;
import com.example.testrxjava.MainActivity;
import com.example.testrxjava.R;
import com.example.testrxjava.base.BaseFragment;
import com.example.testrxjava.login.LoginActivity;
import com.example.testrxjava.util.Action;
import com.example.testrxjava.util.AppManager;
import com.example.testrxjava.util.CommUtil;
import com.example.testrxjava.util.ConfirmDialog;
import com.example.testrxjava.util.SaveCookiesInterceptor;
import com.example.testrxjava.util.SpUtils;

import butterknife.BindView;
import info.hoang8f.widget.FButton;

/**
 * 项目名称：TestRxjava
 *
 * @Author 郭迎辉
 * Create by 10:24 on 2019/4/18.
 * Describe:个人中心页面
 */
public class PersonFragment extends BaseFragment implements View.OnClickListener {
    @BindView(R.id.personLoginButton)
    FButton personLoginButton;
    @BindView(R.id.personUsernameTv)
    TextView personUsernameTv;
    @BindView(R.id.personLogout)
    TextView mPersonLogout;
    @BindView(R.id.collectionRelative)
    RelativeLayout mCollectionRelative;
    @BindView(R.id.settingRelative)
    RelativeLayout mSettingRelative;
    @BindView(R.id.aboutUsRelative)
    RelativeLayout mAboutUsRelative;
    String personUsername;

    @Override
    protected void initData() {

        if(CommUtil.isLogin(getActivity())){
            personUsername= SpUtils.getLoginName(getActivity());//获取登陆的用户名
            personLoginButton.setVisibility(View.GONE);//隐藏登录按钮
            personUsernameTv.setVisibility(View.VISIBLE);//显示用户名
            mPersonLogout.setVisibility(View.VISIBLE);
            personUsernameTv.setText(personUsername);
        }else{

        }

        personLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
//                getActivity().finish();

            }
        });
        mPersonLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                logout();
            }
        });

    }
    private void logout() {//退出登陆
        ConfirmDialog dialog = new ConfirmDialog(getActivity());
        dialog.setOnClickListener(new ConfirmDialog.OnClickListener() {
            @Override
            public void selectSure() {
                Action.getInstance().logout(getActivity(), new LogoutSucessCallback() {
                    @Override
                    public void logoutSucess(LogoutBean result) {
                        Log.e("退出登陆==","退出成功");
                        SpUtils.clearLoginInfo(getActivity());
                        if(SpUtils.getLoginName(getActivity()).equals("")){
                            Intent intent=new Intent(getActivity(), IndexActivity.class);
                            startActivity(intent);
                            AppManager.getInstance().killActivity(getActivity());
                        }
                        SaveCookiesInterceptor.clearCookie(getActivity());
                    }

                    @Override
                    public void logoutFail(String errot) {

                    }
                });



            }

            @Override
            public String setTitle() {
                return getActivity().getString(R.string.dialog_title);
            }

            @Override
            public String setText() {
                return getActivity().getString(R.string.dialog_text);
            }
        });
        dialog.show();
    }
    @Override
    protected View initView() {
        View view= LayoutInflater.from(getActivity()).inflate(R.layout.person_fragment,null);
        return view;
    }

    @Override
    public void onClick(View view) {

    }
}
